package com.livestocksolutions.ui;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JSpinner;
import javax.swing.JSeparator;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.livestocksolutions.LivestockSolutions;
import com.livestocksolutions.util.JDatePickerDate;
import com.livestocksolutions.util.Table;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;
import javax.swing.SwingConstants;
import java.awt.FlowLayout;

/**
 * Show the list of setting options that can be set.
 * @author Ken DeVries
 * @author Adam Parr
 *
 * @.modified 31 November 2012
 */
public class SettingsFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	
	/**
	 * Constructor.
	 */
	public SettingsFrame(){
		setTitle("Settings");
		setIconImage(MainFrame.getImage(MainFrame.APP_ICON));
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		createHomePanel();
		pack();
		setMinimumSize(getSize());
		setLocationRelativeTo(MainFrame.getInstance());
	}
	
	/**
	 * Create the Settings window displaying settings
	 */
	private void createHomePanel(){
		getContentPane().setLayout(new BorderLayout(5, 5));
		
		JPanel settings = new JPanel();
		settings.setBorder(new EmptyBorder(5, 5, 0, 5));
		JPanel buttonPanel = new JPanel();
		JPanel panel = new JPanel();
		
		JSeparator separator = new JSeparator();
		JButton btnCancel = new JButton("Cancel");
		JButton btnApply = new JButton("Apply");

		settings.setLayout(new BoxLayout(settings, BoxLayout.PAGE_AXIS));
		buttonPanel.setLayout(new BorderLayout(0, 0));
		
		panel.add(btnCancel);
		panel.add(btnApply);
		
		buttonPanel.add(separator, BorderLayout.NORTH);
		buttonPanel.add(panel, BorderLayout.SOUTH);
		
		JPanel BackupPanel = new JPanel();
		settings.add(BackupPanel);
		BackupPanel.setLayout(new BoxLayout(BackupPanel, BoxLayout.PAGE_AXIS));
		
		JPanel panel_1 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
		flowLayout.setAlignment(FlowLayout.LEADING);
		BackupPanel.add(panel_1);
		JLabel label_1 = new JLabel("Backup Settings:");
		panel_1.add(label_1);
		
		JPanel backupValuePanel = new JPanel();
		BackupPanel.add(backupValuePanel);
		backupValuePanel.setLayout(new BoxLayout(backupValuePanel, BoxLayout.PAGE_AXIS));
		final JPanel backupPathPanel = new JPanel();
		backupValuePanel.add(backupPathPanel);
		
		JLabel backupPathLabel = new JLabel("Backup Path");
		
		JButton backupBrowse = new JButton("Browse...");
		
		final JTextField backupPath = new JTextField(LivestockSolutions.getBackupPath());
		backupPathPanel.setLayout(new BorderLayout(5, 5));
		
		backupPathPanel.add(backupPathLabel, BorderLayout.WEST);
		backupPathPanel.add(backupPath);
		backupPathPanel.add(backupBrowse, BorderLayout.EAST);
		JPanel backupPeriodPanel = new JPanel();
		backupValuePanel.add(backupPeriodPanel);
		JPanel backupPeriod = new JPanel();
		final JLabel backupEveryLabel = new JLabel("Backup every ");
		final JLabel backupDaysLabel = new JLabel("days");
		final Color forgroundEnabled=backupDaysLabel.getForeground();
		final Color forgroundDisabled=new Color(forgroundEnabled.getRed(), forgroundEnabled.getGreen(), forgroundEnabled.getBlue(), 50);
		
		
		final JSpinner backupDays = new JSpinner();
		backupDays.setModel(new SpinnerNumberModel(new Integer(LivestockSolutions.getBackupPeriod()), new Integer(0), null, new Integer(1)));
		
		backupPeriodPanel.setLayout(new BorderLayout(0, 0));
		
		backupPeriod.add(backupEveryLabel);
		backupPeriod.add(backupDays);
		backupPeriod.add(backupDaysLabel);
		
		JPanel backupPoint = new JPanel();
		backupPeriodPanel.add(backupPoint, BorderLayout.NORTH);
		backupPoint.setLayout(new BorderLayout(0, 0));
		
		final JCheckBox automaticBackup = new JCheckBox("Automatic Backup");
		backupPoint.add(automaticBackup, BorderLayout.WEST);
		automaticBackup.setSelected(true);
		
		JLabel lastBackup = new JLabel("Date of last backup: "+(LivestockSolutions.getLastBackup()==null?"No Previous Backup":JDatePickerDate.format(LivestockSolutions.getLastBackup())));
		backupPoint.add(lastBackup, BorderLayout.EAST);
		
		automaticBackup.addChangeListener(new ChangeListener(){
			@Override
			public void stateChanged(ChangeEvent e) {
				if(automaticBackup.isSelected()){
					backupDays.setEnabled(true);
					backupEveryLabel.setForeground(forgroundEnabled);
					backupDaysLabel.setForeground(forgroundEnabled);
				}else{
					backupDays.setEnabled(false);
					backupEveryLabel.setForeground(forgroundDisabled);
					backupDaysLabel.setForeground(forgroundDisabled);
				}
			}
		});
		backupPeriodPanel.add(backupPeriod, BorderLayout.SOUTH);
		
		((JSpinner.DefaultEditor)backupDays.getEditor()).getTextField().setColumns(10);
		
		backupBrowse.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc = new JFileChooser();
				jfc.setAcceptAllFileFilterUsed(false);
				jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				if(!backupPath.getText().trim().equals("")){
					jfc.setCurrentDirectory(new File(backupPath.getText()));
				}
				if(jfc.showSaveDialog(SettingsFrame.this)==JFileChooser.APPROVE_OPTION){
					backupPath.setText(jfc.getSelectedFile().getAbsolutePath());
				}
			}
		});
		
		JSeparator separator_2 = new JSeparator();
		settings.add(separator_2);
		
		JPanel dateFormatPanel = new JPanel();
		settings.add(dateFormatPanel);
		dateFormatPanel.setLayout(new BoxLayout(dateFormatPanel, BoxLayout.PAGE_AXIS));
		
		JPanel panel_2 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_2.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEADING);
		dateFormatPanel.add(panel_2);

		JLabel label_2 = new JLabel("Date Format Settings:");
		panel_2.add(label_2);
		
		JPanel dateFormatValuePanel = new JPanel();
		dateFormatPanel.add(dateFormatValuePanel);
		
		JLabel lblDateFormat = new JLabel("Display/Import dates in the format of:");
		dateFormatValuePanel.add(lblDateFormat);
		final JTextField dateFormatField = new JTextField(LivestockSolutions.getDateFormat());
		dateFormatValuePanel.add(dateFormatField);
		dateFormatField.setColumns(10);
		
		JButton btnResetToDefault = new JButton("Reset to Default");
		btnResetToDefault.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JDatePickerDate.setDateFormat(null);
				dateFormatField.setText(JDatePickerDate.getDateFormat());
			}
		});
		dateFormatValuePanel.add(btnResetToDefault);
		
		JSeparator separator_3 = new JSeparator();
		settings.add(separator_3);
		
		JPanel tableBackgroundColourPanel = new JPanel();
		settings.add(tableBackgroundColourPanel);
		tableBackgroundColourPanel.setLayout(new BoxLayout(tableBackgroundColourPanel, BoxLayout.PAGE_AXIS));
		
		JPanel panel_3 = new JPanel();
		FlowLayout flowLayout_2 = (FlowLayout) panel_3.getLayout();
		flowLayout_2.setAlignment(FlowLayout.LEADING);
		tableBackgroundColourPanel.add(panel_3);

		JLabel lblTableColourSettings = new JLabel("Table Colour Settings:");
		panel_3.add(lblTableColourSettings);
		
		JPanel tableBackgroundColourValuePanel = new JPanel();
		tableBackgroundColourPanel.add(tableBackgroundColourValuePanel);
		tableBackgroundColourValuePanel.setLayout(new BoxLayout(tableBackgroundColourValuePanel, BoxLayout.X_AXIS));
		
		JPanel eastPanel = new JPanel();
		tableBackgroundColourValuePanel.add(eastPanel);
		eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.PAGE_AXIS));
		
		final JPanel tableBackgroundClickPanel = new JPanel();
		eastPanel.add(tableBackgroundClickPanel);
		final JTextField tableBackgroundClickValue = new JTextField(LivestockSolutions.getColour(LivestockSolutions.LIST_CLICK));
		tableBackgroundClickValue.setColumns(10);
		tableBackgroundClickValue.getDocument().addDocumentListener(new DocumentListener(){
			@Override public void insertUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundClickPanel,tableBackgroundClickValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_CLICK));
			}
			@Override public void removeUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundClickPanel,tableBackgroundClickValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_CLICK));
			}
			@Override public void changedUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundClickPanel,tableBackgroundClickValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_CLICK));
			}});

		tableBackgroundClickPanel.setBackground(Color.decode(tableBackgroundClickValue.getText()));
		tableBackgroundClickPanel.add(new JLabel("Row Click:"));
		tableBackgroundClickPanel.add(tableBackgroundClickValue);
		
		final JPanel tableBackgroundHeaderOrderedPanel = new JPanel();
		eastPanel.add(tableBackgroundHeaderOrderedPanel);
		final JTextField tableBackgroundHeaderOrderedValue = new JTextField(LivestockSolutions.getColour(LivestockSolutions.HEADER_ORDERED));
		tableBackgroundHeaderOrderedValue.setColumns(10);
		tableBackgroundHeaderOrderedValue.getDocument().addDocumentListener(new DocumentListener(){
			@Override public void insertUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundHeaderOrderedPanel,tableBackgroundHeaderOrderedValue,LivestockSolutions.getColourDefault(LivestockSolutions.HEADER_ORDERED));
			}
			@Override public void removeUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundHeaderOrderedPanel,tableBackgroundHeaderOrderedValue,LivestockSolutions.getColourDefault(LivestockSolutions.HEADER_ORDERED));
			}
			@Override public void changedUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundHeaderOrderedPanel,tableBackgroundHeaderOrderedValue,LivestockSolutions.getColourDefault(LivestockSolutions.HEADER_ORDERED));
			}});

		tableBackgroundHeaderOrderedPanel.setBackground(Color.decode(tableBackgroundHeaderOrderedValue.getText()));
		tableBackgroundHeaderOrderedPanel.add(new JLabel("Ordered Header:"));
		tableBackgroundHeaderOrderedPanel.add(tableBackgroundHeaderOrderedValue);
		
		final JPanel tableBackgroundRowOddOddPanel = new JPanel();
		eastPanel.add(tableBackgroundRowOddOddPanel);
		final JTextField tableBackgroundRowOddOddValue = new JTextField(LivestockSolutions.getColour(LivestockSolutions.LIST_A1));
		tableBackgroundRowOddOddValue.setColumns(10);
		tableBackgroundRowOddOddValue.getDocument().addDocumentListener(new DocumentListener(){
			@Override public void insertUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundRowOddOddPanel,tableBackgroundRowOddOddValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_A1));
			}
			@Override public void removeUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundRowOddOddPanel,tableBackgroundRowOddOddValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_A1));
			}
			@Override public void changedUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundRowOddOddPanel,tableBackgroundRowOddOddValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_A1));
			}});

		tableBackgroundRowOddOddPanel.setBackground(Color.decode(tableBackgroundRowOddOddValue.getText()));
		tableBackgroundRowOddOddPanel.add(new JLabel("Odd Row, Odd Cell:"));
		tableBackgroundRowOddOddPanel.add(tableBackgroundRowOddOddValue);
		
		final JPanel tableBackgroundRowEvenOddPanel = new JPanel();
		eastPanel.add(tableBackgroundRowEvenOddPanel);
		final JTextField tableBackgroundRowEvenOddValue = new JTextField(LivestockSolutions.getColour(LivestockSolutions.LIST_B1));
		tableBackgroundRowEvenOddValue.setColumns(10);
		tableBackgroundRowEvenOddValue.getDocument().addDocumentListener(new DocumentListener(){
			@Override public void insertUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundRowEvenOddPanel,tableBackgroundRowEvenOddValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_B1));
			}
			@Override public void removeUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundRowEvenOddPanel,tableBackgroundRowEvenOddValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_B1));
			}
			@Override public void changedUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundRowEvenOddPanel,tableBackgroundRowEvenOddValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_B1));
		}});

		tableBackgroundRowEvenOddPanel.setBackground(Color.decode(tableBackgroundRowEvenOddValue.getText()));
		tableBackgroundRowEvenOddPanel.add(new JLabel("Even Row, Odd Cell:"));
		tableBackgroundRowEvenOddPanel.add(tableBackgroundRowEvenOddValue);
		
		JPanel westPanel = new JPanel();
		tableBackgroundColourValuePanel.add(westPanel);
		westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.PAGE_AXIS));
		
		final JPanel tableBackgroundHoverPanel = new JPanel();
		westPanel.add(tableBackgroundHoverPanel);
		final JTextField tableBackgroundHoverValue = new JTextField(LivestockSolutions.getColour(LivestockSolutions.LIST_HOVER));
		tableBackgroundHoverValue.setColumns(10);
		tableBackgroundHoverValue.getDocument().addDocumentListener(new DocumentListener(){
			@Override public void insertUpdate(DocumentEvent e){
				updateInsertRemoveTableColour(tableBackgroundHoverPanel,tableBackgroundHoverValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_HOVER));
			}
			@Override public void removeUpdate(DocumentEvent e){
				updateInsertRemoveTableColour(tableBackgroundHoverPanel,tableBackgroundHoverValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_HOVER));
			}
			@Override public void changedUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundHoverPanel,tableBackgroundHoverValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_HOVER));
		}});

		tableBackgroundHoverPanel.setBackground(Color.decode(tableBackgroundHoverValue.getText()));
		tableBackgroundHoverPanel.add(new JLabel("Row Hover:"));
		tableBackgroundHoverPanel.add(tableBackgroundHoverValue);
		
		final JPanel tableBackgroundHeaderNormalPanel = new JPanel();
		westPanel.add(tableBackgroundHeaderNormalPanel);
		final JTextField tableBackgroundHeaderNormalValue = new JTextField(LivestockSolutions.getColour(LivestockSolutions.HEADER));
		tableBackgroundHeaderNormalValue.setColumns(10);
		tableBackgroundHeaderNormalValue.getDocument().addDocumentListener(new DocumentListener(){
			@Override public void insertUpdate(DocumentEvent e){
				updateInsertRemoveTableColour(tableBackgroundHeaderNormalPanel,tableBackgroundHeaderNormalValue,LivestockSolutions.getColourDefault(LivestockSolutions.HEADER));
			}
			@Override public void removeUpdate(DocumentEvent e){
				updateInsertRemoveTableColour(tableBackgroundHeaderNormalPanel,tableBackgroundHeaderNormalValue,LivestockSolutions.getColourDefault(LivestockSolutions.HEADER));
			}
			@Override public void changedUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundHeaderNormalPanel,tableBackgroundHeaderNormalValue,LivestockSolutions.getColourDefault(LivestockSolutions.HEADER));
		}});

		tableBackgroundHeaderNormalPanel.setBackground(Color.decode(tableBackgroundHeaderNormalValue.getText()));
		tableBackgroundHeaderNormalPanel.add(new JLabel("Header Normal:"));
		tableBackgroundHeaderNormalPanel.add(tableBackgroundHeaderNormalValue);
		
		final JPanel tableBackgroundRowOddEvenPanel = new JPanel();
		westPanel.add(tableBackgroundRowOddEvenPanel);
		final JTextField tableBackgroundRowOddEvenValue = new JTextField(LivestockSolutions.getColour(LivestockSolutions.LIST_A2));
		tableBackgroundRowOddEvenValue.setColumns(10);
		tableBackgroundRowOddEvenValue.getDocument().addDocumentListener(new DocumentListener(){
			@Override public void insertUpdate(DocumentEvent e){
				updateInsertRemoveTableColour(tableBackgroundRowOddEvenPanel,tableBackgroundRowOddEvenValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_A2));
			}
			@Override public void removeUpdate(DocumentEvent e){
				updateInsertRemoveTableColour(tableBackgroundRowOddEvenPanel,tableBackgroundRowOddEvenValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_A2));
			}
			@Override public void changedUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundRowOddEvenPanel,tableBackgroundRowOddEvenValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_A2));
		}});

		tableBackgroundRowOddEvenPanel.setBackground(Color.decode(tableBackgroundRowOddEvenValue.getText()));
		tableBackgroundRowOddEvenPanel.add(new JLabel("Odd Row, Even Cell:"));
		tableBackgroundRowOddEvenPanel.add(tableBackgroundRowOddEvenValue);
		
		final JPanel tableBackgroundRowEvenEvenPanel = new JPanel();
		westPanel.add(tableBackgroundRowEvenEvenPanel);
		final JTextField tableBackgroundRowEvenEvenValue = new JTextField(LivestockSolutions.getColour(LivestockSolutions.LIST_B2));
		tableBackgroundRowEvenEvenValue.setColumns(10);
		tableBackgroundRowEvenEvenValue.getDocument().addDocumentListener(new DocumentListener(){
			@Override public void insertUpdate(DocumentEvent e){
				updateInsertRemoveTableColour(tableBackgroundRowEvenEvenPanel,tableBackgroundRowEvenEvenValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_B2));
			}
			@Override public void removeUpdate(DocumentEvent e){
				updateInsertRemoveTableColour(tableBackgroundRowEvenEvenPanel,tableBackgroundRowEvenEvenValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_B2));
			}
			@Override public void changedUpdate(DocumentEvent e) {
				updateInsertRemoveTableColour(tableBackgroundRowEvenEvenPanel,tableBackgroundRowEvenEvenValue,LivestockSolutions.getColourDefault(LivestockSolutions.LIST_B2));
		}});
		
		tableBackgroundRowEvenEvenPanel.setBackground(Color.decode(tableBackgroundRowEvenEvenValue.getText()));
		tableBackgroundRowEvenEvenPanel.add(new JLabel("Even Row, Even Cell:"));
		tableBackgroundRowEvenEvenPanel.add(tableBackgroundRowEvenEvenValue);
		
		JButton btnResetDefault = new JButton("Reset to Default");
		btnResetDefault.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				tableBackgroundClickValue.setText(LivestockSolutions.getColourDefault(LivestockSolutions.LIST_CLICK));
				tableBackgroundHeaderOrderedValue.setText(LivestockSolutions.getColourDefault(LivestockSolutions.HEADER_ORDERED));
				tableBackgroundRowOddOddValue.setText(LivestockSolutions.getColourDefault(LivestockSolutions.LIST_A1));
				tableBackgroundRowEvenOddValue.setText(LivestockSolutions.getColourDefault(LivestockSolutions.LIST_B1));
				tableBackgroundHoverValue.setText(LivestockSolutions.getColourDefault(LivestockSolutions.LIST_HOVER));
				tableBackgroundHeaderNormalValue.setText(LivestockSolutions.getColourDefault(LivestockSolutions.HEADER));
				tableBackgroundRowOddEvenValue.setText(LivestockSolutions.getColourDefault(LivestockSolutions.LIST_A2));
				tableBackgroundRowEvenEvenValue.setText(LivestockSolutions.getColourDefault(LivestockSolutions.LIST_B2));
			}
		});
		tableBackgroundColourValuePanel.add(btnResetDefault);
		
		getContentPane().add(settings, BorderLayout.NORTH);
		
		JSeparator separator_4 = new JSeparator();
		settings.add(separator_4);
		
		JPanel deleteData = new JPanel();
		settings.add(deleteData);
		deleteData.setLayout(new BoxLayout(deleteData, BoxLayout.PAGE_AXIS));
		
		JPanel panel_4 = new JPanel();
		FlowLayout flowLayout_3 = (FlowLayout) panel_4.getLayout();
		flowLayout_3.setAlignment(FlowLayout.LEADING);
		deleteData.add(panel_4);
		JLabel label_4 = new JLabel("Data Storage:");
		panel_4.add(label_4);
		
		JPanel DeleteDataValuePanel = new JPanel();
		deleteData.add(DeleteDataValuePanel);
		DeleteDataValuePanel.setLayout(new BoxLayout(DeleteDataValuePanel, BoxLayout.X_AXIS));
		JButton dataDelete = new JButton("Delete all data");
		DeleteDataValuePanel.add(dataDelete);
		dataDelete.setVerticalAlignment(SwingConstants.BOTTOM);
		
		JLabel label = new JLabel("<html><body>Warning: Deleting all data will result in all data used in this application being lost.<br>The only way to recover deleting data is to restore from a previous backup</body></html>");
		DeleteDataValuePanel.add(label);
		
		dataDelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(JOptionPane.showConfirmDialog(SettingsFrame.this,"Are you sure you want to delete all the data from this application?\n\nWarning: Deleting all data will result in all data used in this application being lost.\nThe only way to recover deleting data is to restore from a previous backup","Confirm deleting all data", JOptionPane.WARNING_MESSAGE)==JOptionPane.YES_OPTION){
					LivestockSolutions.deleteAll();
					MainFrame.getInstance().drawTabs();
				}
			}
		});
		getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		btnApply.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ev) {
				boolean success=true;
				if(automaticBackup.isSelected()){
					LivestockSolutions.setBackupPeriod((Integer)backupDays.getValue());
				}else{
					LivestockSolutions.setBackupPeriod(0);
				}
				
				LivestockSolutions.setBackupPath(backupPath.getText());
				
				// First try to set the new date format, if an exception occurs do not try to store it
				try{
					JDatePickerDate.setDateFormat(dateFormatField.getText());
					LivestockSolutions.setDateFormat(dateFormatField.getText());
				}catch(Exception e){
					success=false; 
					JOptionPane.showMessageDialog(SettingsFrame.this,"Date format: "+dateFormatField.getText()+" is not a valid date.\n\nAccepted values include:\nDay: dd (1-31), dddd (Sunday-Saturday)\nMonth: MM (1-12), MMM (January-December)\nYear: yy (12) - year of 2012, yyyy (2012)" ,"Entered date format is not valid", JOptionPane.OK_OPTION);
				}
				
				LivestockSolutions.setColour(LivestockSolutions.LIST_CLICK, tableBackgroundClickValue.getText());
				LivestockSolutions.setColour(LivestockSolutions.LIST_HOVER, tableBackgroundRowEvenOddValue.getText());
				LivestockSolutions.setColour(LivestockSolutions.HEADER_ORDERED, tableBackgroundHeaderOrderedValue.getText());
				LivestockSolutions.setColour(LivestockSolutions.HEADER, tableBackgroundHeaderNormalValue.getText());
				LivestockSolutions.setColour(LivestockSolutions.LIST_A1, tableBackgroundRowOddOddValue.getText());
				LivestockSolutions.setColour(LivestockSolutions.LIST_B1, tableBackgroundHoverValue.getText());
				LivestockSolutions.setColour(LivestockSolutions.LIST_A2, tableBackgroundRowOddEvenValue.getText());
				LivestockSolutions.setColour(LivestockSolutions.LIST_B2, tableBackgroundRowEvenEvenValue.getText());
				
				Table.updateColours(
					LivestockSolutions.getColour(LivestockSolutions.HEADER_ORDERED),
					LivestockSolutions.getColour(LivestockSolutions.HEADER),
					LivestockSolutions.getColour(LivestockSolutions.LIST_HOVER),
					LivestockSolutions.getColour(LivestockSolutions.LIST_CLICK),
					LivestockSolutions.getColour(LivestockSolutions.LIST_A1),
					LivestockSolutions.getColour(LivestockSolutions.LIST_A2),
					LivestockSolutions.getColour(LivestockSolutions.LIST_B1),
					LivestockSolutions.getColour(LivestockSolutions.LIST_B2));
				MainFrame.getInstance().setSelectedTab(MainFrame.getInstance().getSelectedTab());
				if(success){
					dispose();
				}
			}
		});
		if((Integer)backupDays.getValue()==0){
			automaticBackup.doClick();
		}
	}
	
	private static Color getColour(String colourValue, String defaultValue){
		colourValue=colourValue.trim();
		try{
			return Color.decode(!colourValue.equals("")&&!colourValue.equals("#")?colourValue:defaultValue);
		}catch(Exception e){
			return Color.decode(defaultValue);
		}
	}
	
	private static void updateInsertRemoveTableColour(JPanel backgroundPanel, JTextField value, String defaultValue){
		backgroundPanel.setBackground(getColour(value.getText(),defaultValue));
	}
	
}
